package org.example.simple_1.web;

import lombok.RequiredArgsConstructor;
import org.example.simple_1.stateMachineConfig.OrderStateMachine;
import org.springframework.stereotype.Service;
import org.springframework.statemachine.StateMachine;

@Service
@RequiredArgsConstructor
public class OrderService {



    public void handleEvent(String orderId, String event) throws Exception {
        OrderStateEntity entity = new OrderStateEntity().setOrderId("123L").setCurrentState(OrderStateMachine.States.PROCESSING);


        StateMachine<String, String> machine = OrderStateMachine.buildMachine(entity.getCurrentState());

        // 启动状态机
        machine.start();

        // 发送事件（同步方式）
        machine.sendEvent(event);

        // 获取新状态并保存到数据库
        String newState = machine.getState().getId();
        System.out.println("==========="+newState);
        entity.setCurrentState(newState);

        // 模拟的数据库从操作,这里没有我就注掉了
        // orderStateMapper.save(entity);
    }


    public String getCurrentState(String orderId) {
        // 这里应该啊是查询数据库的,   不查了,给一个mock值吧
        // return orderStateMapper.findById(orderId)
        //         .map(OrderStateEntity::getCurrentState)
        //         .orElse("Not Found");

        return "Not Found";
    }
}